#include "mainwindow.h"
#include <screenw.h>
#include <spritesw.h>
#include <QMenuBar>
#include <QAction>
#include <QDockWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QCloseEvent>
#include <QDebug>


CMainWindow::CMainWindow(): QMainWindow( )
{

	m_pActNewProject = new QAction( tr( "New" ), this );
	m_pActLoadProject = new QAction( tr( "Load" ), this );
	m_pActSaveProject = new QAction( tr( "Save" ), this );
	m_pActSaveAsProject = new QAction( tr( "Save as..." ), this );
	m_pActCloseProject = new QAction( tr( "Close" ), this );
	m_pActQuit = new QAction( tr( "Quit" ), this );
	
	m_pActNewScreen = new QAction( tr( "New" ), this );
	m_pActLoadScreen = new QAction( tr( "Load" ), this );
	m_pActSaveScreen = new QAction( tr( "Save" ), this );
	m_pActSaveAsScreen = new QAction( tr( "Save as..." ), this );
	m_pActExportScreen = new QAction( tr( "Export to ASM..." ), this );
	
	m_pActNewTiles = new QAction( tr( "New" ), this );
	m_pActLoadTiles = new QAction( tr( "Load" ), this );
	m_pActSaveTiles = new QAction( tr( "Save" ), this );
	m_pActSaveAsTiles = new QAction( tr( "Save as..." ), this );
	m_pActExportTiles = new QAction( tr( "Export ASM..." ), this );
	
	m_pActNewSprites = new QAction( tr( "New" ), this );
	m_pActLoadSprites = new QAction( tr( "Load" ), this );
	m_pActSaveSprites = new QAction( tr( "Save" ), this );
	m_pActSaveAsSprites = new QAction( tr( "Save as..." ), this );
	m_pActCloseSprites = new QAction( tr( "Close" ), this );
	m_pActExportSprites = new QAction( tr( "Export..." ), this );
	
	m_pActAbout = new QAction( tr( "nMSXtiles" ), this );

	connect( m_pActNewProject, SIGNAL( triggered() ), this, SLOT( OnNewProject() ) );
	connect( m_pActLoadProject, SIGNAL( triggered() ), this, SLOT( OnLoadProject() ) );
	connect( m_pActSaveProject, SIGNAL( triggered() ), this, SLOT( OnSaveProject() ) );
	connect( m_pActSaveAsProject, SIGNAL( triggered() ), this, SLOT( OnSaveAsProject() ) );
	connect( m_pActCloseProject, SIGNAL( triggered() ), this, SLOT( OnCloseProject() ) );
	connect( m_pActQuit, SIGNAL( triggered() ), this, SLOT( OnQuit() ) );
	
	connect( m_pActNewScreen, SIGNAL( triggered() ), this, SLOT( OnNewScreen() ) );
	connect( m_pActLoadScreen, SIGNAL( triggered() ), this, SLOT( OnLoadScreen() ) );
	connect( m_pActSaveScreen, SIGNAL( triggered() ), this, SLOT( OnSaveScreen() ) );
	connect( m_pActSaveAsScreen, SIGNAL( triggered() ), this, SLOT( OnSaveAsScreen() ) );
	connect( m_pActExportScreen, SIGNAL( triggered() ), this, SLOT( OnExportScreen() ) );
	
	connect( m_pActNewTiles, SIGNAL( triggered() ), this, SLOT( OnNewTiles() ) );
	connect( m_pActLoadTiles, SIGNAL( triggered() ), this, SLOT( OnLoadTiles() ) );
	connect( m_pActSaveTiles, SIGNAL( triggered() ), this, SLOT( OnSaveTiles() ) );
	connect( m_pActSaveAsTiles, SIGNAL( triggered() ), this, SLOT( OnSaveAsTiles() ) );
	connect( m_pActExportTiles, SIGNAL( triggered() ), this, SLOT( OnExportTiles() ) );

	connect( m_pActNewSprites, SIGNAL( triggered() ), this, SLOT( OnNewSprites() ) );
	connect( m_pActLoadSprites, SIGNAL( triggered() ), this, SLOT( OnLoadSprites() ) );
	connect( m_pActSaveSprites, SIGNAL( triggered() ), this, SLOT( OnSaveSprites() ) );
	connect( m_pActSaveAsSprites, SIGNAL( triggered() ), this, SLOT( OnSaveAsSprites() ) );
	connect( m_pActCloseSprites, SIGNAL( triggered() ), this, SLOT( OnCloseSprites() ) );
	connect( m_pActExportSprites, SIGNAL( triggered() ), this, SLOT( OnExportSprites() ) );
	
	connect( m_pActAbout, SIGNAL( triggered() ), this, SLOT( OnAbout() ) );
	
	m_pMenu = menuBar()->addMenu( tr( "&Project" ) );
	m_pMenu->addAction( m_pActNewProject );
	m_pMenu->addAction( m_pActLoadProject );
	m_pMenu->addAction( m_pActSaveProject );
	m_pMenu->addAction( m_pActSaveAsProject );
	m_pMenu->addAction( m_pActCloseProject );
	m_pMenu->addAction( m_pActQuit );
	
	m_pMenu = menuBar()->addMenu( tr( "Screen" ) );
	m_pMenu->addAction( m_pActNewScreen );
	m_pMenu->addAction( m_pActLoadScreen );
	m_pMenu->addAction( m_pActSaveScreen );
	m_pMenu->addAction( m_pActSaveAsScreen );
	m_pMenu->addAction( m_pActExportScreen );
	
	
	m_pMenu = menuBar()->addMenu( tr( "Tiles" ) );
	m_pMenu->addAction( m_pActNewTiles );
	m_pMenu->addAction( m_pActLoadTiles );
	m_pMenu->addAction( m_pActSaveTiles );
	m_pMenu->addAction( m_pActSaveAsTiles );
	m_pMenu->addAction( m_pActExportTiles );
	
	m_pMenu = menuBar()->addMenu( tr( "Sprites" ) );
	m_pMenu->addAction( m_pActNewSprites );
	m_pMenu->addAction( m_pActLoadSprites );
	m_pMenu->addAction( m_pActSaveSprites );
	m_pMenu->addAction( m_pActSaveAsSprites );
	m_pMenu->addAction( m_pActCloseSprites );
	m_pMenu->addAction( m_pActExportSprites );
	
	m_pMenu = menuBar()->addMenu( tr( "About" ) );
	m_pMenu->addAction( m_pActAbout );

	m_pWks = new QWorkspace;
	setCentralWidget( m_pWks );

	m_pScreen = NULL;
	m_pSprites = NULL;

	resize( 450, 600 );

	m_ProjectFile = "";
	m_ScreenFile = "";
	m_TilesFile = "";
	
	EnableMenuItems();
}


void CMainWindow::OnNewProject()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsProject();	
	
	delete m_pScreen;
	
	m_pScreen = new CScreenW();
	m_pScreen->Initialize();
	m_pWks->addWindow( m_pScreen, Qt::Dialog );
	m_pScreen->show();
	m_pScreen->NewTiles();
	m_pScreen->NewScreen();

	m_ProjectFile = "";
	m_ScreenFile = "";
	m_TilesFile = "";

	EnableMenuItems();
}

void CMainWindow::OnLoadProject()
{
	QFile hFile;
	QString fileName;
	QFileDialog dlg;

	
	if( m_pScreen ) OnCloseProject();
	
	fileName = dlg.getOpenFileName();

	if( fileName == "" ) return;

	m_ProjectFile = fileName;
	
	hFile.setFileName( m_ProjectFile );
	hFile.open( QIODevice::ReadOnly );

	QTextStream str( &hFile );

	if( hFile.atEnd() ) 
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}
	
	if( str.readLine() != "PROJECT" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}

	if( str.readLine() != "SCREEN" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}

	m_ScreenFile = str.readLine();

	m_pScreen = new CScreenW();
	m_pScreen->Initialize();
	m_pWks->addWindow( m_pScreen, Qt::Dialog );
	m_pScreen->show();

	m_pScreen->LoadScreen( m_ScreenFile );
	
	if( str.readLine() != "TILES" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}
	
	m_TilesFile = str.readLine();
	
	m_pScreen->LoadTiles( m_TilesFile );

	hFile.close();
	
	EnableMenuItems();
	
}

void CMainWindow::OnSaveProject()
{
	if( m_ProjectFile == "" ) OnSaveAsProject();
	else SaveProject();
}

void CMainWindow::OnSaveAsProject()
{
	QString fileName;
	QFileDialog dlg;

	
	if( m_ScreenFile == "" ) OnSaveAsScreen();
	else m_pScreen->SaveScreen( m_ScreenFile );
	
	if( m_TilesFile == "" ) OnSaveAsTiles();
	else m_pScreen->SaveTiles( m_TilesFile );
	
	if( m_ScreenFile == "" || m_TilesFile == "" ) 
	{
		QMessageBox::question( this, "Project not saved!", "Warning" );
		return; 
	}

	m_ProjectFile = dlg.getSaveFileName( this, "Project - Save as" );

	if( m_ProjectFile > "" ) SaveProject();
	else QMessageBox::question( this, "Project not saved!", "Warning" );	
}

void CMainWindow::SaveProject()
{
	QFile hFile;
	
	
	if( m_ProjectFile == "" ) OnSaveAsProject();
	else
	{	
		OnSaveScreen();
		OnSaveTiles();
					
		hFile.setFileName( m_ProjectFile );
		hFile.open( QIODevice::WriteOnly );

		QTextStream str( &hFile );
		
		str<<"PROJECT"<<endl;
		str<<"SCREEN"<<endl;
		str<<m_ScreenFile<<endl;
		str<<"TILES"<<endl;
		str<<m_TilesFile<<endl;
		hFile.flush();
		hFile.close();
	}
}

void CMainWindow::OnCloseProject()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveProject();	

	m_pScreen->Close();
	delete m_pScreen;
	m_pScreen = NULL;

	EnableMenuItems();
}

void CMainWindow::OnNewScreen()
{
	OnSaveScreen();
	m_pScreen->NewScreen();
}

void CMainWindow::OnLoadScreen()
{
	QFile hFile;
	QString line;
	QString fileName;
	QFileDialog dlg;


	fileName = dlg.getOpenFileName();

	if( fileName == "" ) return;
	
	m_ScreenFile = fileName;

	m_pScreen->LoadScreen( m_ScreenFile );
}


void CMainWindow::OnSaveScreen()
{
	if( m_ScreenFile == "" ) OnSaveAsScreen();
	else m_pScreen->SaveScreen( m_ScreenFile );
}

void CMainWindow::OnSaveAsScreen()
{
	QFileDialog dlg;
	QString fileName;

	
	fileName = dlg.getSaveFileName( this, "Screen - Save as" );

	if( fileName > "" ) 
	{
		m_ScreenFile = fileName;
		m_pScreen->SaveScreen( m_ScreenFile );
	}
}

void CMainWindow::OnExportScreen()
{
	QFileDialog dlg;
	QString fileName;
	
	
	fileName = dlg.getSaveFileName( this, "Screen - Export to ASM as" );
	
	if( fileName > "" ) m_pScreen->ExportScreen( fileName );
}

void CMainWindow::OnQuit()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsProject();	
	
	if( m_pScreen ) m_pScreen->Close();	
	
	close();
}

void CMainWindow::OnAbout()
{
	QMessageBox::about( this, "nMSXtiles", "Screens editor for MSX screen 2\n\nVersion 0.2.1\n\nRamon de las Heras Gomiz\nwww.pentacour.com" );
}

void CMainWindow::OnNewTiles()
{
	OnSaveTiles();
	m_pScreen->NewTiles();
}

void CMainWindow::OnLoadTiles()
{
	QFile hFile;
	QString line;
	QFileDialog dlg;
	QString fileName;


	fileName = dlg.getOpenFileName();

	if( fileName == "" ) return;
	
	m_TilesFile = fileName;

	m_pScreen->LoadTiles( m_TilesFile );
}

void CMainWindow::OnSaveTiles()
{
	if( m_TilesFile == "" ) OnSaveAsTiles();
	else m_pScreen->SaveTiles( m_TilesFile );
}

void CMainWindow::OnSaveAsTiles()
{
	QFileDialog dlg;
	QString fileName;


	fileName = dlg.getSaveFileName( this, "Tiles - Save as" );

	if( fileName > "" ) 
	{
		m_TilesFile = fileName;
		m_pScreen->SaveTiles( m_TilesFile );
	}
}

void CMainWindow::OnExportTiles()
{
	QFileDialog dlg;
	QString fileName;
	
	
	fileName = dlg.getSaveFileName( this, "Tiles - Export to ASM as" );
	
	if( fileName > "" ) m_pScreen->ExportTiles( fileName );
}

void CMainWindow::OnNewSprites()
{
	if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsSprites();	
	
	delete m_pSprites;
	
	m_pSprites= new CSpritesW();
	m_pSprites->Initialize();
	m_pWks->addWindow( m_pSprites, Qt::Dialog );
	m_pSprites->show();
	m_pSprites->NewSprites();

	m_SpritesFile = "";

	EnableMenuItems();
}

void CMainWindow::OnLoadSprites()
{
	QFile hFile;
	QString line;
	QFileDialog dlg;
	QString fileName;


	fileName = dlg.getOpenFileName();

	if( fileName == "" ) return;
	
	m_SpritesFile = fileName;
	/////
//AQUI		if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsSprites();	
	
	if( m_pSprites ) delete m_pSprites; //AQUI
	
	m_pSprites= new CSpritesW();
	m_pSprites->Initialize();
	m_pWks->addWindow( m_pSprites, Qt::Dialog );
	m_pSprites->show();
	m_pSprites->LoadSprites( m_SpritesFile );

	EnableMenuItems();
}

void CMainWindow::OnSaveSprites()
{
	if( m_SpritesFile == "" ) OnSaveAsSprites();
	else m_pSprites->SaveSprites( m_SpritesFile );
}

void CMainWindow::OnSaveAsSprites()
{
	QFileDialog dlg;
	QString fileName;


	fileName = dlg.getSaveFileName( this, "Sprites - Save as" );

	if( fileName > "" ) 
	{
		m_SpritesFile = fileName;
		m_pSprites->SaveSprites( m_SpritesFile );
	}
}

void CMainWindow::OnCloseSprites()
{
	int i;


	if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveSprites();	

	m_pSprites->Close();

	delete m_pSprites;
	m_pSprites = NULL;

	EnableMenuItems();

}

void CMainWindow::OnExportSprites()
{
	QFileDialog dlg;
	QString fileName;
	
	
	fileName = dlg.getSaveFileName( this, "Sprites - Export as" );
	
	if( fileName > "" ) m_pSprites->ExportSprites( fileName );
}

void CMainWindow::EnableMenuItems()
{

	m_pActLoadProject->setEnabled( true );
	m_pActSaveProject->setEnabled( m_pScreen != NULL );
	m_pActSaveAsProject->setEnabled( m_pScreen != NULL );
	m_pActCloseProject->setEnabled( m_pScreen != NULL );

	m_pActNewScreen->setEnabled( m_pScreen!= NULL );
	m_pActLoadScreen->setEnabled( m_pScreen != NULL );
	m_pActSaveScreen->setEnabled( m_pScreen != NULL );
	m_pActSaveAsScreen->setEnabled( m_pScreen != NULL );
	m_pActExportScreen->setEnabled( m_pScreen != NULL );
	
	m_pActNewTiles->setEnabled( m_pScreen != NULL );
	m_pActLoadTiles->setEnabled( m_pScreen != NULL );
	m_pActSaveTiles->setEnabled( m_pScreen != NULL );
	m_pActSaveAsTiles->setEnabled( m_pScreen != NULL );
	m_pActExportTiles->setEnabled( m_pScreen != NULL );
	
	m_pActNewSprites->setEnabled( true );
	m_pActLoadSprites->setEnabled( true );
	m_pActSaveSprites->setEnabled( m_pSprites != NULL );
	m_pActSaveAsSprites->setEnabled( m_pSprites != NULL );
	m_pActCloseSprites->setEnabled( m_pSprites != NULL );
	m_pActExportSprites->setEnabled( m_pSprites != NULL );
}

